package com.winhxd.b2c.order.controller;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.order.condition.OrderPayCallBackCondition;
import com.winhxd.b2c.common.domain.order.condition.OrderRefundCallbackCondition;
import com.winhxd.b2c.common.exception.BusinessException;
import com.winhxd.b2c.common.feign.order.OrderCallBackClient;
import com.winhxd.b2c.common.util.JsonUtil;
import com.winhxd.b2c.order.service.OrderPayService;
import com.winhxd.b2c.order.service.OrderRefundService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 订单支付、退款， 回调
 * @author: liuhanning
 * @create: 2019-01-10 19:33
 **/
@RestController
public class OrderCallBackController implements OrderCallBackClient {
    private static final Logger logger = LoggerFactory.getLogger(OrderCallBackController.class);

    @Autowired
    private OrderRefundService orderRefundService;

    @Autowired
    private OrderPayService orderPayService;

    @Override
    public ResponseResult<Boolean> callbackOrderPay(@RequestBody  OrderPayCallBackCondition condition) {
        ResponseResult<Boolean> result= new ResponseResult<>();
        Boolean flag = orderPayService.orderPayCallBack(condition);
        result.setData(flag);
        return result;
    }

    @Override
    public ResponseResult<Boolean> callbackOrderRefund(@RequestBody  OrderRefundCallbackCondition condition) {
        if(null == condition){
            throw new BusinessException(BusinessCode.CODE_406101);
        }
        String logTitle = "/orderPay/4067/v1/callbackOrderRefund-售后订单:退款回调接口";
        ResponseResult<Boolean> result = new ResponseResult<>();
        logger.info("{}=--开始--{}",logTitle, JsonUtil.toJSONString(condition));
        if (StringUtils.isBlank(condition.getOutRefundNo())) {
            logger.info(logTitle+"--退款流水号为空");
            result.setData(false);
            return result;
        }
        Boolean res = orderRefundService.orderRefundCallBack(condition);
        result.setData(res);
        return result;
    }
}
